<template>
    <div class="wait-main-layout">
        <yd-navbar height="0.9rem"
                   title="点点维"
                   fontsize=".3rem"
                   class="wait-navbar-part">
            <img slot="right"
                 class="history-logo-part"
                 src="../../assets/task_history_icon.png" @click="showHistoryHandler"/>
        </yd-navbar>

        <div style="height: 0.9rem"></div>

        <PopWindowDialog style="position: fixed;top:0;right: 0;margin-top: 0.9rem"
                         :isShowPopDialog="isShowPopDialog"
                         v-on:pop-handler="popHandlerClick"
                         :popDatas="popWinList"/>
        <yd-pullrefresh :callback="loadList" ref="pullrefreshDemo" :show-init-tip="false">

            <div>
                <div v-if="waitList.length>0" >
                    <div v-for="(item,index) in waitList" class="bottom-line-part">
                        <RichTextComponent
                                v-on:rich-handler="richHandlerClick"
                                :taskObj="item">
                            <!--报修单-->
                            <div v-if="item.type === 'warning'"
                                 slot="slot">
                                <div v-if="item.state==='14'&&item.userUuid===null"
                                     class="wait-double-mobule-part gray-text-color">
                                    <span v-on:click="awaitOneHandler(item)">查看详情</span>
                                    <span v-on:click="awaitTwoHandler(item)">归档</span>
                                </div>
                                <div v-else-if="item.state==='98'"
                                     class="wait-double-mobule-part gray-text-color">
                                    <span v-on:click="awaitOneHandler(item)">查看详情</span>
                                    <span v-on:click="awaitTwoHandler(item)">立即评价</span>
                                </div>
                                <div v-else
                                     class="wait-mobule-part gray-text-color">
                                    <span v-on:click="awaitDetailHandler(item)">查看详情</span>
                                </div>
                            </div>
                            <!--场地巡检-->
                            <div v-if="item.type === 'inspection'"
                                 slot="slot">
                                <div v-if="item.state==='20'"
                                     class="wait-double-mobule-part gray-text-color">
                                    <span v-on:click="awaitOneHandler(item)">查看报告</span>
                                    <span v-on:click="awaitTwoHandler(item)">确认报告</span>
                                </div>
                                <div v-else-if="item.state==='90'"
                                     class="wait-double-mobule-part gray-text-color">
                                    <span v-on:click="awaitOneHandler(item)">查看报告</span>
                                    <span v-on:click="awaitTwoHandler(item)">立即评价</span>
                                </div>
                                <div v-else
                                     class="wait-mobule-part gray-text-color">
                                    <span v-on:click="awaitDetailHandler(item)">查看详情</span>
                                </div>
                            </div>
                            <!--采购单-->
                            <div v-if="item.type === 'commodity'"
                                 slot="slot">
                                <div v-if="item.state==='03'"
                                     class="wait-double-mobule-part gray-text-color">
                                    <span v-on:click="awaitOneHandler(item)">查看详情</span>
                                    <span v-on:click="awaitTwoHandler(item)">确认收货</span>
                                </div>
                                <div v-else-if="item.state==='95'"
                                     class="wait-double-mobule-part gray-text-color">
                                    <span v-on:click="awaitOneHandler(item)">查看详情</span>
                                    <span v-on:click="awaitTwoHandler(item)">立即评价</span>
                                </div>
                                <div v-else
                                     class="wait-mobule-part gray-text-color">
                                    <span v-on:click="awaitDetailHandler(item)">查看详情</span>
                                </div>
                            </div>
                            <!--凭证请领-->
                            <div v-if="item.type === 'voucher'"
                                 slot="slot">
                                <div v-if="item.state==='03'"
                                     class="wait-double-mobule-part gray-text-color">
                                    <span v-on:click="awaitOneHandler(item)">查看详情</span>
                                    <span v-on:click="awaitTwoHandler(item)">确认收货</span>
                                </div>
                                <div v-else-if="item.state==='95'"
                                     class="wait-double-mobule-part gray-text-color">
                                    <span v-on:click="awaitOneHandler(item)">查看详情</span>
                                    <span v-on:click="awaitTwoHandler(item)">立即评价</span>
                                </div>
                                <div v-else
                                     class="wait-mobule-part gray-text-color">
                                    <span v-on:click="awaitDetailHandler(item)">查看详情</span>
                                </div>
                            </div>
                        </RichTextComponent>
                        <div class="divide-line-part"></div>
                    </div>
                </div>
                <div v-else>
                    <img src="../../assets/task_none_task_icon.png" class="nolist-image" alt="">
                    <p  class="nolist-tite">没有待办事项</p>
                </div>
            </div>

        </yd-pullrefresh>

        <div style="height: 1.5rem"></div>
    </div>
</template>

<script>
    import RichTextComponent from "@/components/RichTextComponent.vue";
    import PopWindowDialog from "@/components/PopWindowDialog.vue";
    import {todoList,sendEvaluate,getProInfoFromOrder} from "../../pcof/post";
    import {historyObject,userInfoData,isEditRepairOrder,repairOrderData,repairOrderSavedInstance,instObject,providerObject,qn_domain} from '../../globalVariables'
    export default {
        name: "wait",
        data() {
            return {
                waitList:[],
                types:["设备报修", "场地巡检", "办公用品","凭证请领"],
                isShowPopDialog: false,
                popWinList: [
                    {id: 0,type: 'warning',text:'设备报修'},
                    {id: 1,type: 'inspection',text:'场地巡检'},
                    {id: 2,type: 'commodity',text:'办公用品'},
                    {id: 3,type: 'voucher',text:'凭证请领'}
                ]
            }
        },
        beforeCreate() {
            this.$dialog.loading.open('加载中...');
        },
        created() {
            this.qn_domain = localStorage.getItem(qn_domain);
            this.todoListFunc()
        },
        components: {
            RichTextComponent,
            PopWindowDialog,
        },
        methods: {
            showHistoryHandler() {
                this.isShowPopDialog = !this.isShowPopDialog;
            },
            popHandlerClick(payload) {
                console.log(payload.popObject);
                if (payload.popObject.type === 'warning') {
                    this.$router.push({name: 'waitHistory'});
                    localStorage.removeItem(historyObject);
                    var  historyObj = new Object();
                    historyObj.historyType = 'warningHistory';
                    historyObj.tab = 0;
                    historyObj.typeIndex = 0;
                    localStorage.setItem(historyObject,JSON.stringify(historyObj));
                } else if (payload.popObject.type === 'inspection') {
                    this.$router.push({name: 'waitHistory'});
                    localStorage.removeItem(historyObject);
                    var  historyObj = new Object();
                    historyObj.historyType = 'inspectionHistory';
                    historyObj.tab = 0;
                    historyObj.typeIndex = 1;
                    localStorage.setItem(historyObject,JSON.stringify(historyObj));
                } else if (payload.popObject.type === 'commodity') {
                    this.$router.push({name: 'waitHistory'});
                    localStorage.removeItem(historyObject);
                    var  historyObj = new Object();
                    historyObj.historyType = 'commodityHistory';
                    historyObj.tab = 0;
                    historyObj.typeIndex = 2;
                    localStorage.setItem(historyObject,JSON.stringify(historyObj));
                } else if (payload.popObject.type === 'voucher') {
                    this.$router.push({name: 'waitHistory'});
                    localStorage.removeItem(historyObject);
                    var  historyObj = new Object();
                    historyObj.historyType = 'voucherHistory';
                    historyObj.tab = 0;
                    historyObj.typeIndex = 3;
                    localStorage.setItem(historyObject,JSON.stringify(historyObj));
                }
            },
            async todoListFunc() {
                var response = await todoList(JSON.parse(localStorage.getItem(userInfoData)).mobile);
                if (response.data.code === 200) {
                    this.$dialog.loading.close();
                    this.waitList = [];
                    this.waitList = response.data.data;
                } else {
                    this.$dialog.loading.close();
                    this.$dialog.toast({
                        mes: response.data.msg,
                        timeout: 1500
                    });
                }
            },
            async loadList() {
                var response = await todoList(JSON.parse(localStorage.getItem(userInfoData)).mobile);
                if (response.data.code === 200) {
                    this.$dialog.loading.close();
                    this.waitList = [];
                    this.waitList = response.data.data;
                } else {
                    this.$dialog.loading.close();
                    this.$dialog.toast({
                        mes: response.data.msg,
                        timeout: 1500
                    });
                }
                this.$refs.pullrefreshDemo.$emit('ydui.pullrefresh.finishLoad');
            },
            richHandlerClick(payload) {
                if (payload.taskObj.type === 'warning') {
                    if (payload.taskObj.source === '2') {
                        //打开维修单详情界面
                        //清理录入维修单相关的缓存
                        localStorage.removeItem(repairOrderSavedInstance);
                        localStorage.removeItem(repairOrderData);
                        localStorage.removeItem(isEditRepairOrder);
                        localStorage.removeItem(instObject);
                        localStorage.removeItem(providerObject);
                        localStorage.setItem(isEditRepairOrder,'0');
                        localStorage.setItem(repairOrderData,JSON.stringify(payload.taskObj));
                        console.log(payload.taskObj);
                        this.$router.push({name: "repairOrder"});
                    } else {
                        //打开报修单详情界面
                        this.$router.push({name: 'repairTask', params: {taskUUid: payload.taskObj.uuid,taskState: payload.taskObj.state,isShowEvaluateDialog: false}});
                    }
                } else if (payload.taskObj.type === 'inspection'){
                    this.$router.push({name: 'inspectionSite', params: {taskUUid: payload.taskObj.uuid,taskState: payload.taskObj.state,isShowEvaluateDialog: false}})
                }  else if (payload.taskObj.type === 'commodity'){
                    //进入办公用品页
                    //type 0是待办页 1是历史页来的 currentHandler 当前的操作对象 applierUuid 发起人
                    this.$router.push({name: 'officeDetailandprogress', params: {orderUuid: payload.taskObj.uuid,state:payload.taskObj.state,currentHandler:payload.taskObj.currentHandler,applierUuid:payload.taskObj.userUuid,type:'0'}});


                }  else if (payload.taskObj.type === 'voucher'){
                    //进入凭证请领界面
                    this.$router.push({name: 'voucherDetailandProgress', params: {applicationUuid: payload.taskObj.uuid,state:payload.taskObj.state,type:'0'}});

                } else if (payload.taskObj.type === 'message') {
                    this.$router.push({name: 'systemMessage'});


                }
            },
            awaitDetailHandler(item) {
                if (item.type === 'warning') {
                    this.$router.push({name: 'repairTask', params: {taskUUid: item.uuid,taskState: item.state,isShowEvaluateDialog: false}});
                } else if (item.type === 'inspection') {
                    this.$router.push({name: 'inspectionSite', params: {taskUUid: item.uuid}})
                } else if (item.type === 'commodity') {
                    //type 0是待办页 1是历史页来的 currentHandler 当前的操作对象 applierUuid 发起人
                    this.$router.push({name: 'officeDetailandprogress', params: {orderUuid: item.uuid,state:item.state,currentHandler:item.currentHandler,applierUuid:item.userUuid,type:'0'}});

                } else if (item.type === 'voucher') {
                    //进入凭证请领界面
                    this.$router.push({name: 'voucherDetailandProgress', params: {applicationUuid: item.uuid,state:item.state,type:'0'}});

                }
            },
            awaitOneHandler(item) {
                if (item.type === 'warning') {
                    this.$router.push({name: 'repairTask', params: {taskUUid: item.uuid,taskState: item.state,isShowEvaluateDialog: false}});
                } else if (item.type === 'inspection') {
                    this.$router.push({name: 'inspectionSite', params: {taskUUid: item.uuid,taskState: item.state,isShowEvaluateDialog: false}})
                } else if (item.type === 'commodity') {
                    //type 0是待办页 1是历史页来的 currentHandler 当前的操作对象 applierUuid 发起人
                    this.$router.push({name: 'officeDetailandprogress', params: {orderUuid: item.uuid,state:item.state,currentHandler:item.currentHandler,applierUuid:item.userUuid,type:'0'}});

                } else if (item.type === 'voucher') {
                    //type 0是待办页 1是历史页来的
                    this.$router.push({name: 'voucherDetailandProgress', params: {applicationUuid: item.uuid,state:item.state,type:'0'}});

                }
            },
            awaitTwoHandler(item) {
                console.log(item);
                //报修单立即评价
                if (item.type === 'warning') {
                    if (item.state === '14') {
                        //打开维修单详情界面
                        //清理录入维修单相关的缓存
                        localStorage.removeItem(repairOrderSavedInstance);
                        localStorage.removeItem(repairOrderData);
                        localStorage.removeItem(isEditRepairOrder);
                        localStorage.removeItem(instObject);
                        localStorage.removeItem(providerObject);
                        localStorage.setItem(isEditRepairOrder,'0');
                        localStorage.setItem(repairOrderData,JSON.stringify(item));
                        this.$router.push({name: "repairOrder"});
                    } else if (item.state === '98') {
                        //打开报修单进度界面并弹出评价对话框
                        this.$router.push({name: 'repairTask', params: {taskUUid: item.uuid,taskState: item.state,isShowEvaluateDialog: true}});
                    }
                }
                //场地巡检确认报告和立即评价
                else if (item.type === 'inspection') {
                    if (item.state === '20') {
                        this.$router.push({name: 'inspectionSite', params: {taskUUid: item.uuid,taskState: item.state,isShowEvaluateDialog: false}});
                    } else if (item.state === '90') {
                        this.$router.push({name: 'inspectionSite', params: {taskUUid: item.uuid,taskState: item.state,isShowEvaluateDialog: true}});
                    }
                }
                //采购单确认收货
                else if (item.type === 'commodity' && item.state==='03') {

                        //确认收货
                    //type 0是待办页 1是历史页来的 currentHandler 当前的操作对象 applierUuid 发起人
                    // this.$router.push({name: 'officeDetailandprogress', params: {orderUuid: item.uuid,state:item.state,currentHandler:item.currentHandler,applierUuid:item.userUuid,type:'0'}});

                    this.$router.push({name: 'voucherShouhuonum', params: {applicationUuid: item.uuid,dataAr:item.commodities,type:'1'}});//1 是办公用品 2是凭证请领
                }
                //采购单立即评价
                else if (item.type === 'commodity' && item.state==='95') {

                    this.$router.push({name: 'customEvaluate', params: {type: '1',uuid: item.uuid}});//type //1 采购单是办公用品 2是凭证请领

                }
                //凭证请领确认收货
                else if (item.type === 'voucher' && item.state==='03') {
                    this.$router.push({name: 'voucherShouhuonum', params: {applicationUuid: item.uuid,dataAr:item.vouchers,type:'2'}});//1 是办公用品 2是凭证请领

                    // this.$router.push({name: 'voucherDetail', params: {applicationUuid: item.uuid,state:item.state,type:'0'}});

                }
                //凭证请领立即评价
                else if (item.type === 'voucher' && item.state==='95') {
                    this.$router.push({name: 'customEvaluate', params: {type: '2',uuid: item.uuid}});//type //1 是办公用品 2是凭证请领

                }
            },
            async repairEvaluateClick(item) {
                if (item.type === 'warning') {
                    var response = await sendEvaluate(
                        JSON.parse(localStorage.getItem(userInfoData)).mobile,
                        this.taskUUid,
                        this.jineng*2,
                        this.sudu*2,
                        this.fuwu*2,
                        JSON.parse(localStorage.getItem(userInfoData)).uuid,
                        this.desContent
                    );
                    if (response.data.code === 200) {
                        this.$router.back();
                    } else {
                        this.$dialog.toast({
                            mes: response.data.msg,
                            icon: 'error',
                            timeout: 1500
                        });
                    }
                }
            },
            async getProInfoFromOrderFunc() {
                var response = await getProInfoFromOrder(
                    JSON.parse(localStorage.getItem(userInfoData)).mobile,
                    this.taskUUid,
                );
                if (response.data.code === 200) {
                    this.headUrl = response.data.data.head_image_url;
                    this.userName = response.data.data.name;
                } else {
                    this.$dialog.toast({
                        mes: response.data.msg,
                        icon: 'error',
                        timeout: 1500
                    });
                }
            },
        },
    }
</script>

<style lang="scss">
    .wait-main-layout {

        .wait-navbar-part {
            z-index:200;
            position: fixed;
            width: 100%;
            top: 0;
            left: 0;
        }

        .history-logo-part {
            width: 0.5rem;
            height: 0.4rem;
        }

        .wait-mobule-part {
            padding-top: 0.1rem;
            padding-bottom: 0.1rem;
            font-size: 1.2em;
        }

        .wait-double-mobule-part {
            display: flex;
            padding-top: 0.1rem;
            padding-bottom: 0.1rem;
            font-size: 1.2em;
            justify-content: space-around;
        }
    }
</style>